package regex.script.compiler;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

/**
 * User: regex | Kyle
 * Date: 25/07/11
 * Time: 10:19
 */
public class Compiler {
    private File file;
    @SuppressWarnings({"FieldCanBeLocal"})  //This is going to be updated later so it can't be local!
    private String type;

    public Compiler(File f) {
        file = f;
    }

    public void loadUnit() {
        try {
            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            final DocumentBuilder bldr = factory.newDocumentBuilder();
            final Document doc = bldr.parse(file);
            doc.getDocumentElement().normalize();
            final NodeList list = doc.getElementsByTagName("compiling-unit");
            for(int i = 0; i < list.getLength(); i++) {
                Element elmnt = (Element) list.item(i);
                NodeList compilers = elmnt.getElementsByTagName("compiler");
                for(int k = 0; k < compilers.getLength(); k++) {
                    Element comp_elmnt = (Element) compilers.item(k);
                    String type = comp_elmnt.getAttribute("type");
                    setType(type);
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void setType(String type) {
        this.type = type;
    }
}
